import 'package:flutter/material.dart';
import 'package:flutterlib/common/style/quhui_style.dart';
import 'package:flutterlib/widgets/pull/nested/gif_pull_header.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutterlib/common/utils/utils.dart';


typedef  RenderHeader = List<Widget> Function(BuildContext context, bool innerBoxIsScrolled);
typedef  RenderFooter = List<Widget> Function(BuildContext context, bool innerBoxIsScrolled);
///通用下上刷新控件
class PullLoadWidget extends StatefulWidget {
  ///item渲染
  final IndexedWidgetBuilder itemBuilder;

  final WidgetBuilder emptyBuilder;
  ///加载更多回调
  final RefreshCallback onLoadMore;

  ///下拉刷新回调
  final RefreshCallback onRefresh;

  ///控制器，比如数据和一些配置
  final PullLoadWidgetControl control;

  final RenderHeader headerSliverBuilder;

  final RenderFooter footerSliverBuilder;

  final ScrollController scrollController;

  final RefreshController refreshController;

  final bool enablePullDown;

  final bool enablePullUp;

  final double itemExtend;

  PullLoadWidget(
      this.control, this.itemBuilder, this.onRefresh, this.onLoadMore,
      {
      this.headerSliverBuilder,
        this.footerSliverBuilder,
      this.scrollController,
      this.refreshController,
      this.enablePullDown,
      this.enablePullUp, this.itemExtend, this.emptyBuilder});

  @override
  _PullLoadWidgetState createState() =>
      _PullLoadWidgetState(
        this.control,
        this.itemBuilder,
        this.onRefresh,
        this.onLoadMore,
      );
}

class _PullLoadWidgetState
    extends State<PullLoadWidget> {
  final IndexedWidgetBuilder itemBuilder;

  final RefreshCallback onLoadMore;

  final RefreshCallback onRefresh;


  PullLoadWidgetControl control;

  int headerSize = 0;

  _PullLoadWidgetState(this.control, this.itemBuilder,
      this.onRefresh, this.onLoadMore);

  @override
  void initState() {
    super.initState();
    headerSize = widget.headerSliverBuilder(context, false).length;
  }

  _getListCount() {
    ///当数据为空的时候，返回空页面

    if(control.dataList.length == 0){
      return 1 + headerSize;
    }
    return control.dataList.length + headerSize;
  }

  ///根据配置状态返回实际列表渲染Item
  _getItem(int index) {
    ///回调外部正常渲染Item，如果这里有需要，可以直接返回相对位置的index
    if(index < headerSize){
      return widget.headerSliverBuilder(context, false)[index];
    }
    if(control.dataList.length == 0 && index == headerSize){
      return widget.emptyBuilder != null ? widget.emptyBuilder(context) : _buildEmpty();
    }
    return itemBuilder(context, index - headerSize);
  }

  void _onOffsetCallback(bool isUp, double offset) {}

  @override
  Widget build(BuildContext context) {

    return SmartRefresher(
      controller: widget.refreshController,
      enablePullDown: widget.enablePullDown,
      enablePullUp: widget.enablePullUp,
      onRefresh: onRefresh,
      footer: ClassicFooter(
        loadStyle: LoadStyle.ShowWhenLoading,
        noDataText: "到了世界的尽头了...",
        loadingText: "努力加载中，请稍等",


      ),
      header: GifPullHeader(),
      onLoading: () {
        // if (this.control.needLoadMore.value) {
          this.onLoadMore?.call();
        // }
      },
      onOffsetChange: _onOffsetCallback,
      child: ListView.builder(
        physics: BouncingScrollPhysics(),
        itemBuilder: (BuildContext context, int position) {
          return _getItem(position);
        },
        itemExtent: widget.itemExtend,
        itemCount: _getListCount(),
      ),
    );
  }

  ///空页面
  Widget _buildEmpty() {
    return new Container(
      height: 350.h,
      alignment: Alignment.center,
      child: new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          RaisedButton.icon(
            icon: Icon(Icons.refresh),
            label: Text("请刷新"),
            onPressed: () {widget.refreshController?.requestRefresh();},
          ),
          Container(
            child: Text("数据为空", style: QuhuiConstant.normalText),
          ),
        ],
      ),
    );
  }

}

 class PullLoadWidgetControl {
   ///数据，对齐增减，不能替换
   List dataList = new List();

   ///是否需要加载更多
   ValueNotifier<bool> needLoadMore = new ValueNotifier(false);
 }
